package com.zjmzxfzhl.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.zjmzxfzhl.modules.app.form.AppAlarmUserVo;
import com.zjmzxfzhl.modules.app.form.MeetTaskVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.entity.AppAlarmUser;
import com.zjmzxfzhl.modules.app.mapper.AppAlarmUserMapper;
import com.zjmzxfzhl.modules.app.service.AppAlarmUserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppAlarmUserServiceImpl extends BaseServiceImpl<AppAlarmUserMapper, AppAlarmUser>
        implements AppAlarmUserService {
    @Override
    public IPage<AppAlarmUserVo> list(IPage<AppAlarmUserVo> page, AppAlarmUserVo appAlarmUserVo) {
        return page.setRecords(baseMapper.list(page, appAlarmUserVo));
    }
    @Override
    public List<AppAlarmUserVo> getList(IPage<AppAlarmUserVo> page, AppAlarmUserVo appAlarmUserVo) {
        return baseMapper.list(page, appAlarmUserVo);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppAlarmUser(AppAlarmUser appAlarmUser) {
        Map<SFunction<AppAlarmUser,?>,Object> params=new HashMap<>();
        if(appAlarmUser.getSetId()!=null&&!"".equals(appAlarmUser.getSetId())){
            params.put(AppAlarmUser::getSetId,appAlarmUser.getSetId());
        }
        if(appAlarmUser.getUserId()!=null&&!"".equals(appAlarmUser.getUserId())){
            params.put(AppAlarmUser::getUserId,appAlarmUser.getUserId());
        }
        Integer total=this.count(new LambdaQueryWrapper<AppAlarmUser>().allEq(params));
        if(total>0){
            return false;
        }
        appAlarmUser.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        Boolean result=this.save(appAlarmUser);
        return result;
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appAlarmUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean relationDel(AppAlarmUser appAlarmUser) {
        Map<String, Object> columnMap=new HashMap<>();
        if (appAlarmUser.getSetId() == null && appAlarmUser.getUserId() == null) {
            return false;
        }
        if(appAlarmUser.getSetId()!=null){
            columnMap.put("SET_ID",appAlarmUser.getSetId());
        }
        if(appAlarmUser.getUserId()!=null){
            columnMap.put("USER_ID",appAlarmUser.getUserId());
        }
        Boolean result=this.removeByMap(columnMap);
        return result;
    }

    /**
     * 获取事务提醒用户
     *
     * @return
     */
    public String getAlarmSetUser(String setId) {
        String userIds=baseMapper.getAlarmSetUser(setId);
        return userIds;
    }
}
